---
layout: default
title: Creating a service
permalink: /creating-a-service/
redirect_from:
  - /creating_a_service.html
sitemap:
    priority: 0.7
    lastmod: 2014-02-17T00:00:00-00:00
---

<h1><i class="fa fa-bolt"></i> Creating a service</h1>

<h2>Introduction</h2>
<p>
  This is a very simple sub-generator, compared to the <a href="{{ site.url }}/creating-an-entity/">"entity" sub-generator</a>.
</p>
<p>
  Its goal is to generate a Spring Service bean, which is where your application's business logic is supposed to be coded.
</p>
<p>
  In order to generate a "bar" Service bean, just type:
</p>
<p>
  <code>
  yo jhipster:service bar
  </code>
</p>
<p>
  <i>
  This will generate a simple "BarService": very few lines of codes, but they usually come with a lot of questions. We are trying to answer the most common ones below.
  </i>
</p>

<h2>Why aren't Service classes generated by the "entity" generator?</h2>

<p>
  We have two main architecture principles here:
</p>
<ul>
  <li>We don't want to promote creating useless Services: if all you need is a basic CRUD on your database, you don't need a Service bean. That's why, by default, JHipster doesn't generate them.</li>
  <li>We believe a Service bean to be more coarse-grained than a repository. A Service bean will use several repositories to provide business value on top of them. That's why Service beans cannot be generated with Entities.</li>
</ul>

<h2>Should we use interfaces with our Service Beans?</h2>
<p>
  Short answer: <b>No</b>.
</p>
<p>
  If you want the long answer, here it is:
</p>
<p>
  One of the main interests of using Spring is AOP. This is the technology that allows Spring to add new behaviors on top of your Beans: for instance, this is how transactions or security work.
</p>
<p>
  In order to add those behaviors, Spring needs to create a proxy on your class, and there are two ways of creating a proxy:
</p>
<ul>
  <li>If your class uses an interface, Spring will use a standard mechanism provided by Java to create a dynamic proxy.</li>
  <li>If your class doesn't use an interface, Spring will use CGLIB to generate a new class on the fly: this is not a standard Java mechanism, but it works as well as the standard mechanism.</li>
</ul>
<p>
  Some people will also argue that interfaces are better for writing tests, but we believe we shouldn't modify our production code for tests, and that all the new mocking frameworks (like EasyMock) allow you to create very good unit tests without any interfaces.
</p>
<p>
  So, in the end, we find that interfaces for your Service beans are mostly useless, and that's why we don't recommend them (but we leave you with the option to generate them!).
</p>

<h2>Why should I use transactions to fetch lazy JPA relationships?</h2>
<p>
  By default JPA uses lazy initialization of one-to-many and many-to-many entity relationships. If you use this default configuration, you will probably see the dreaded <code>LazyInitializationException</code>: it means you have tried to use an un-initialized relationship outside of a transaction.
</p>
<p>
  As the generated Service class has by default the <code>@Transactional</code> annotation, all of its methods are transactional. This means that you can fetch all the required lazy relationship inside those business methods, without any  <code>LazyInitializationException</code>.
</p>
<p>
  <i>Tip:</i> use <code>@Transactional(readOnly = true)</code> on a method if you are not modifying any data. This is a nice performance optimization (Hibernate won't need to flush its 1st level cache, as we are not modifying anything), as well as a quality enhancement with some JDBC drivers (Oracle won't allow you to send INSERT/UPDATE/DELETE statements)
</p>

<h2>Can we add security to Service Beans?</h2>

<p>
  Yes! Just add Spring Security's <code>@Secured</code> annotation on your class or on your methods, and use the provided AuthoritiesConstants class to restrict access to specific user authorities.
</p>

<h2>Can we monitor Service Beans?</h2>

<p>
  Yes! Just add Metrics' <code>@Timed</code> annotations on the methods you want to monitor.
</p>
